package ee.ttu.lecturerate.http;

import org.json.JSONException;
import org.json.JSONObject;

import ee.ttu.lecturerate.data.Comment;

public class RequestAssembler
{
	private static final String destination = "http://1.lecturerate.appspot.com";
	
	public static RequestInput assembleGetUser(String username)
	{
		RequestInput input = new RequestInput();
		input.setType(1);
		input.setUrl(destination + "/lecture");
		input.addParameter("mode", "user");
		input.addParameter("action", "get");
		input.addParameter("username", username);
		
		return input;
	}
	
	public static RequestInput assembleGetLectureList()
	{
		RequestInput input = new RequestInput();
		input.setType(2);
		input.setUrl(destination + "/lecture");
		input.addParameter("mode", "lecture");
		input.addParameter("action", "get");
		
		return input;
	}
	
	public static RequestInput assembleGetLecture(String id)
	{
		RequestInput input = new RequestInput();
		input.setType(3);
		input.setUrl(destination + "/lecture");
		input.addParameter("mode", "lecture");
		input.addParameter("action", "get");
		input.addParameter("id", id);
		
		return input;
	}
	
	public static RequestInput assemblePutComment(Comment comment, String lectureId)
	{
		RequestInput input = new RequestInput();
		input.setType(4);
		input.setUrl(destination + "/lecture");
		input.addParameter("mode", "rating");
		input.addParameter("action", "store");
		
		JSONObject jObj = new JSONObject();
		try {
			jObj.put("username", comment.getAuthor());
			jObj.put("comment", comment.getComment());
			jObj.put("rating", comment.getRating());
			jObj.put("lectureId", lectureId);
			input.addParameter("rating", jObj.toString());
		}
		catch (JSONException e) {
			//ignore - Server will discard the request if "rating" parameter is not found
		}
		
		return input;
	}
	
	public static RequestInput assembleGetCommentsByLecture(String lectureId)
	{
		RequestInput input = new RequestInput();
		input.setType(5);
		input.setUrl(destination + "/lecture");
		input.addParameter("mode", "rating");
		input.addParameter("action", "get");
		input.addParameter("lectureId", lectureId);
		
		return input;
	}
	
}